/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.GUI.MenuMain;

import edu.nii.qlks.ControlLayer.TaiKhoanControl;
import edu.nii.qlks.DataTransferObject.TaiKhoanInfo;
import edu.nii.qlks.GUI.Menu;
import java.awt.Color;
import java.awt.Image;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author MARS_NHO
 */
public class Login extends javax.swing.JFrame {
    
    TaiKhoanControl taiKhoanCT;
    TaiKhoanInfo taiKhoanInfo;
    Menu menu;
    
    /**
     * Creates new form Login
     */
    public Login() {
        initComponents();
        Image icon = getToolkit().getImage(getClass().getResource("/images/key.png"));
        setIconImage(icon);
        
        taiKhoanCT = new TaiKhoanControl();
        taiKhoanInfo = new TaiKhoanInfo();
        
        
        
        float[] color = new float[3];
        color = Color.RGBtoHSB(255, 255, 255, color);
        this.getContentPane().setBackground(Color.getHSBColor(color[0], color[1], color[2]));
 
        //SET O GIUA MAN HINH
        setLocationRelativeTo(null);
    }
    
    public void LayMenu(Menu _menu){
        menu = _menu;
    }
    
    public void Xuly(){
        String str_user = jXTextField_TenDangNhap.getText();
        String str_pass;
        str_pass = new String(jPasswordField_MatKhau.getPassword());
         
        if (str_user.equals("")){
            JOptionPane.showMessageDialog(rootPane, "Vui lòng nhập tên đăng nhập!", "Lỗi", JOptionPane.ERROR_MESSAGE);
            jXTextField_TenDangNhap.requestFocus();
            return;
        }
        if (str_pass.equals("")){
            JOptionPane.showMessageDialog(rootPane, "Vui lòng nhập mật khẩu!", "Lỗi", JOptionPane.ERROR_MESSAGE);
            jPasswordField_MatKhau.requestFocus();
            return;
        }
        
        Vector vt = new Vector();
        try {
            vt = taiKhoanCT.LayTT(str_user, str_pass);
            if (vt.size() > 0){
                //JOptionPane.showMessageDialog(rootPane, "Đăng nhập thành công!", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                Vector vt_row = (Vector)vt.get(0);
                
                String str = vt_row.get(0).toString().trim();
                taiKhoanInfo.setMaTaiKhoan(str);
                str = vt_row.get(1).toString().trim();
                taiKhoanInfo.setTenTaiKhoan(str);
                str = vt_row.get(2).toString().trim();
                taiKhoanInfo.setMaLoaiTaiKhoan(str);
                str = vt_row.get(3).toString().trim();
                taiKhoanInfo.setMatKhau(str);
                
                menu = new Menu(taiKhoanInfo);
                this.dispose();
                this.menu.setVisible(true);
            }
            else{
                JOptionPane.showMessageDialog(rootPane, "Đăng nhập không thành công!", "Lỗi", JOptionPane.ERROR_MESSAGE);
                jXTextField_TenDangNhap.requestFocus();
                jPasswordField_MatKhau.setText(null);
                return;
            }
        } catch (Exception ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXLabel_TieuDe = new org.jdesktop.swingx.JXLabel();
        jXButton_DangNhap = new org.jdesktop.swingx.JXButton();
        jXButton_Thoat = new org.jdesktop.swingx.JXButton();
        jXLabel_TenDangNhap = new org.jdesktop.swingx.JXLabel();
        jXLabel_MatKhau = new org.jdesktop.swingx.JXLabel();
        jPasswordField_MatKhau = new javax.swing.JPasswordField();
        jXTextField_TenDangNhap = new org.jdesktop.swingx.JXTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Đăng nhập");
        setBackground(new java.awt.Color(255, 255, 255));
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setResizable(false);

        jXLabel_TieuDe.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/loginbanner.png"))); // NOI18N
        jXLabel_TieuDe.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

        jXButton_DangNhap.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); // NOI18N
        jXButton_DangNhap.setText("Đăng nhập");
        jXButton_DangNhap.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jXButton_DangNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXButton_DangNhapActionPerformed(evt);
            }
        });

        jXButton_Thoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/no.png"))); // NOI18N
        jXButton_Thoat.setText("Thoát");
        jXButton_Thoat.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jXButton_Thoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXButton_ThoatActionPerformed(evt);
            }
        });

        jXLabel_TenDangNhap.setText("Tên đăng nhập");
        jXLabel_TenDangNhap.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jXLabel_MatKhau.setText("Mật khẩu");
        jXLabel_MatKhau.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jPasswordField_MatKhau.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordField_MatKhauActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXLabel_TieuDe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jXLabel_TenDangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jXLabel_MatKhau, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jXTextField_TenDangNhap, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPasswordField_MatKhau, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(83, 83, 83)
                        .addComponent(jXButton_DangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(jXButton_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXLabel_TieuDe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXLabel_TenDangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXTextField_TenDangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordField_MatKhau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel_MatKhau, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXButton_DangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXButton_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jXButton_ThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXButton_ThoatActionPerformed
        System.exit(0);
        
    }//GEN-LAST:event_jXButton_ThoatActionPerformed

    private void jXButton_DangNhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXButton_DangNhapActionPerformed
        Xuly();
    }//GEN-LAST:event_jXButton_DangNhapActionPerformed

    private void jPasswordField_MatKhauActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordField_MatKhauActionPerformed
        jXButton_DangNhapActionPerformed(evt);
    }//GEN-LAST:event_jPasswordField_MatKhauActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Login().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPasswordField jPasswordField_MatKhau;
    private org.jdesktop.swingx.JXButton jXButton_DangNhap;
    private org.jdesktop.swingx.JXButton jXButton_Thoat;
    private org.jdesktop.swingx.JXLabel jXLabel_MatKhau;
    private org.jdesktop.swingx.JXLabel jXLabel_TenDangNhap;
    private org.jdesktop.swingx.JXLabel jXLabel_TieuDe;
    private org.jdesktop.swingx.JXTextField jXTextField_TenDangNhap;
    // End of variables declaration//GEN-END:variables
}
